package zyc.web;


import zyc.pojo.User;
import zyc.service.UserService;
import zyc.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @description:
 * @author: ZhaoYicong
 * @date: Created in 2020/7/16 8:10
 * @version: v1.0
 * @modified By:
 */
public class RegistServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        System.out.println(username);
        String password = req.getParameter("password");
        String email = req.getParameter("email");
        String code = req.getParameter("code");
        System.out.println(code);

        User user=new User(null,username,password,email);

        UserService userService=new UserServiceImpl();

        if ("abcd".equals(code)){

            boolean b = userService.exsitsUser(username);
            if (b){
                req.setAttribute("masg","用户名已经存在");
                System.out.println("用户名已经存在");
                req.setAttribute("username",username);
                req.setAttribute("email",email);
                req.getRequestDispatcher("/pages/user/regist.jsp").forward(req,resp);

            }else{
                System.out.println("注册成功");
                userService.registUser(user);
                req.getRequestDispatcher("/pages/user/regist_success.jsp").forward(req,resp);
            }
        }else{
            req.setAttribute("masg","验证码错误");
            req.setAttribute("username",username);
            req.setAttribute("email",email);
            req.getRequestDispatcher("/pages/user/regist.jsp").forward(req,resp);
        }

    }
}
